package com.traffic.between.adaptor;


import java.util.Date;

import org.apache.log4j.Logger;

import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.YouMiServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;


@ChooseProvider(channel = "youmi")
public class YouMiAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(YouMiAdaptor.class);

	
	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		String productId = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String cstmOrderNo = orderBean.getIds();
		String operator = ""; 
		String flowType = "";
		if ("3".equals(orderBean.getOperator())) {//联通
			operator = "2";
		}else if ("2".equals(orderBean.getOperator())) {//移动
			operator = "1";
		}else if ("1".equals(orderBean.getOperator())) {
			operator ="3";//电信
		}else {
			
		}
		if ("0".equals(orderBean.getRegion())) {
			flowType = "1";
		}else if ("1".equals(orderBean.getRegion())) {
			flowType = "2";
		}else{
			
		}
		String result = YouMiServiceImpl.service.order(productId, phoneNo, cstmOrderNo,operator,flowType);
		log.info("bsm- 杭州有米YouMiService.order() 订购请求result<>:" + result);
		JSONObject json = JSONObject.fromObject(result);

		int status = json.getInt("status");
		
		
		if ("1000".equals(String.valueOf(status))) {
			orderBean.setStatus("11");
			orderBean.setMessage(json.getString("statusMessage"));
			orderBean.setErrorMsg(json.getString("statusMessage"));
		} else {
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("statusMessage"));
			orderBean.setErrorMsg(json.getString("statusMessage"));
		}		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	public void status() {
		OrderBean orderBean = OrderInfoContext.getContext();
		String cstmOrderNo = orderBean.getIds();
      
		String result = YouMiServiceImpl.service.orderStatus(cstmOrderNo);

		JSONObject json = JSONObject.fromObject(result);

		int status  = json.getInt("status");

	
		if ("1000".equals(String.valueOf(status))) {
			String data = json.getString("data");
			JSONObject dataJson = JSONObject.fromObject(data);
			int orderStatus = dataJson.getInt("orderStatus");
			if ("0".equals(String.valueOf(orderStatus))||"2".equals(String.valueOf(orderStatus))) {
				orderBean.setStatus("8");
				orderBean.setMessage("订购失败");
				orderBean.setErrorMsg("订购失败");
			}else if ("1".equals(String.valueOf(orderStatus))) {
				orderBean.setStatus("7");
				orderBean.setMessage("订购成功");
				orderBean.setErrorMsg("订购成功");
	
			}else {
				orderBean.setStatus("1");
				orderBean.setMessage("订购中");
				orderBean.setErrorMsg("订购中");

			}
		}else {
			orderBean.setStatus("1");
			orderBean.setMessage("订购中");
			orderBean.setErrorMsg("订购中");
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
